package com.niji.littly_donkey_android.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.*;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.niji.littly_donkey_android.R;
import com.niji.littly_donkey_android.http.ConnectViaSession;
import com.niji.littly_donkey_android.util.JsonUtil;
import com.niji.littly_donkey_android.util.StatusBarUtil;
import com.niji.littly_donkey_android.util.UrlUtil;
import org.json.JSONException;

import java.util.HashMap;
import java.util.Map;

public class FeedBackActivity extends BaseActivity {

    private final static String TAG = "FeedBackActivity";

    /**
     * 返回按钮
     */
    private Button feedback_return;

    /**
     * 输入框初始值
     */
    private int num = 0;

    /**
     * 输入框最大值
     */
    private int maxNum = 200;

    /**
     * 输入框内容
     */
    private EditText feedback_editText;

    /**
     * 显示实施数字
     */
    private TextView feedback_number;


    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feedback_activity);
        feedbackReturn();
        feddbackInput();
        feedbackSubmit();
    }

    /**
     * 提交反馈
     */
    private void feedbackSubmit() {
        Button feed_back_btn  = findViewById(R.id.feed_back_btn);
        feed_back_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = feedback_editText.getText().toString().trim();
                if(content.equals("")){
                    Toast.makeText(FeedBackActivity.this,"反馈内容不能为空",Toast.LENGTH_SHORT).show();
                }else {
                    String url = UrlUtil.URL + "/platform/feedback?content=" + content;
                    RequestQueue requestQueue= Volley.newRequestQueue(FeedBackActivity.this);
                    StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                        @Override
                        public void onResponse(String s) {
                            try {
                                Map<String, Object> map = JsonUtil.jsonToMap(s);
                                Integer code = (Integer) map.get("code");
                                if (code == 0) {
                                    String data = (String) map.get("data");
                                    Log.i(TAG,data);
                                    Toast.makeText(FeedBackActivity.this, data, Toast.LENGTH_LONG).show();
                                    overridePendingTransition(R.anim.xpage_slide_in_left, R.anim.xpage_slide_out_right);
                                    finish();

                                } else if(code == -1){
                                    Toast.makeText(FeedBackActivity.this, "登录失效，请重新登录", Toast.LENGTH_LONG).show();
                                    overridePendingTransition(R.anim.xpage_slide_in_left, R.anim.xpage_slide_out_right);
                                    Intent intent = new Intent(FeedBackActivity.this, GetPhoneCodeActivity.class);
                                    intent.putExtra("loginCode","-1");
                                    startActivity(intent);
                                    finish();
                                }else {
                                    String data = (String) map.get("data");
                                    Toast.makeText(FeedBackActivity.this, data, Toast.LENGTH_SHORT).show();
                                    overridePendingTransition(R.anim.xpage_slide_in_left, R.anim.xpage_slide_out_right);
                                    finish();
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
//
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
                            Log.e(TAG, volleyError.getMessage(), volleyError);
                        }
                    }) {
                        @Override
                        public Map<String, String> getHeaders() throws AuthFailureError {
                            String sesseionid = new ConnectViaSession(FeedBackActivity.this).GetSession();
                            if (!sesseionid.equals("")) {
                                HashMap<String, String> headers = new HashMap<String, String>();
                                headers.put("cookie", sesseionid);
                                return headers;
                            } else {
                                return super.getHeaders();
                            }
                        }
                    };

                    requestQueue.add(stringRequest);
                }
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.feedback_activity;

    }
    @Override
    protected int getStatusBarColor() {
        return R.color.white_easy_photos;
    }
    /**
     * 输入框监听
     */
    private void feddbackInput() {
        feedback_editText = findViewById(R.id.feedback_editText);
        feedback_number = findViewById(R.id.feedback_number);
        feedback_editText.addTextChangedListener(new TextWatcher() {
            //记录输入的字数
            private CharSequence wordNum;
            private int selectionStart;
            private int selectionEnd;
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                wordNum = s;

            }

            @Override
            public void afterTextChanged(Editable s) {
                int number = num + s.length();
//                TextView显示剩余字数
                feedback_number.setText("" + number +"/200");
                selectionStart = feedback_editText.getSelectionStart();
                selectionEnd = feedback_editText.getSelectionEnd();
                //判断大于最大值
                if (wordNum.length() > maxNum) {
                    //删除多余输入的字（不会显示出来）
                    s.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = s.length();
                    feedback_editText.setSelection(tempSelection);//设置光标在最后
                    Toast.makeText(FeedBackActivity.this, "最多输入200个字", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    private void feedbackReturn() {
        feedback_return = findViewById(R.id.feedback_return);
        feedback_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                overridePendingTransition(R.anim.xpage_slide_in_left,R.anim.xpage_slide_out_right);
                finish();
            }
        });

    }
}
